//
//  DHDataBaseFactory.swift
//  dataBaseProj
//
//  Created by sea on 2020/1/7.
//  Copyright © 2020 CDSCRB. All rights reserved.
//

import UIKit
import FMDB


var queue:FMDatabaseQueue? = nil
var manager:DHDataBaseFactory? = nil

/// 创建db factory
class DHDataBaseFactory: NSObject {

    @objc static let defaultManager:DHDataBaseFactory = {
         manager = DHDataBaseFactory()
        return manager!
    }()
    
    
    @objc func creatDataBase() -> FMDatabaseQueue{
        if queue == nil {
            let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
            let documentDirectory = paths[0];
            let dbPath = documentDirectory.appending("/footBallTable.db")
            
            queue = FMDatabaseQueue.init(path: dbPath)
            print("数据库地址\(dbPath)")
        }

        return queue!
    }
    
    @objc func isExistWithTableNameInDataBase(name:String,dataBase:FMDatabase)->(Bool){
       
        var isOK  = false
        var rs:FMResultSet?
//        var newError:NSError?
        do {
        
            try  rs = dataBase.executeQuery("select count(*) as 'count' from sqlite_master where type ='table' and name = '\(name)'", values:nil)
        } catch  {
            print("error here")
        }
        
        while rs!.next() {
            let count = rs!.int(forColumn: "count")
            if 0 == count {
                isOK = false
            }else{
                isOK = true
            }
        }
        rs!.close()
        return isOK;
   }
    
    
}
